# -*- coding:utf-8 -*-
import web, models
from baseHandler import BaseHandler
from utils.decorator import render

class Login(BaseHandler):
    @render('login.html')
    def GET(self):
        tips = web.input().get('tips')
        return {'title' : '登陆', 'tips' : tips}

    def POST(self):
        username = web.input().get('username')
        password = web.input().get('password')
        user = models.User.getUserByNamePass(username, password)
        if user:
            web.ctx.session.login = True
            web.ctx.session.username = user.username
            web.ctx.session.user = user
            raise web.seeother('/')
        else:
            tips = u'帐号不存在或密码错误!'
            raise web.seeother('/login?tips=%s' % tips)
